package com.nondo.dean.platform.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Province;
import com.nondo.dean.platform.model.AreaBean;
import com.nondo.dean.platform.service.ProvinceService;

/**
 * 省份管理后台接口
 * @author zlj
 * @date 2018年4月3日
 */
@RestController("apiAdminProvince")
@RequestMapping("/{v}/admin/province")
public class ProvinceController {
    
    @Autowired
    ProvinceService provinceService;
    
    /**
     * 查看省份详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Province> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<Province> view = new View<Province>(20000, "获取数据成功");
        
        Province province = null;
        if (id != null) {
            province = provinceService.selectProvince(id);
        } else {
            province = new Province();
        }

        view.setData(province);
        return view;
    }
    
    /**
     * 添加省份
     * @param request
     * @param response
     * @param province 省份信息 {@link Province}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Province> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody Province province, BindingResult result) {
        View<Province> view = new View<Province>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Province provinceDb = this.provinceService.selectProvinceByName(province.getName());
            if (provinceDb != null) {
                return View.of500Error("省份名称已存在");
            }
            this.provinceService.addProvince(province);
        }
        return view;
    }

    /**
     * 更新省份
     * @param request
     * @param response
     * @param province 省份信息 {@link Province}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Province> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody Province province, BindingResult result) {
        View<Province> view = new View<Province>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Province provinceDb = provinceService.selectProvinceByName(province.getName());
            if (provinceDb.getName().equals(province.getName()) && provinceDb.getId() != province.getId()) {
                return View.of500Error("省份名称已存在");
            }
            this.provinceService.updateProvince(provinceDb);
        }

        return view;
    }

    /**
     * 获取省份分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Province>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Province> query) {
        View<Page<Province>> view = new View<Page<Province>>(20000, "获取列表成功");

        Province province = query.getData();

        Page<Province> page = new Page<Province>(query.getPage(), query.getLimit(), provinceService.count(province));
        
        List<Province> provinceList = provinceService.getProvinceList(query.getOffset(), query.getLimit(), province);
        
        page.setContent(provinceList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除省份
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        Province province = provinceService.selectProvince(id);
        province.setDelFlag(String.valueOf(1));
        this.provinceService.updateProvince(province);

        return view;
    }
    
    /**
     * 获取省份列表
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/all")
    public View<List<AreaBean>> list(HttpServletRequest request, HttpServletResponse response) {
        View<List<AreaBean>> view = new View<List<AreaBean>>(20000, "获取列表成功");

        List<AreaBean> provinceList = provinceService.getProvinceList();
        
        view.setData(provinceList);
        
        return view;
    }
}